blog

Home / DeveloperSection / Blogs / How to use HTML Video tag

How to use HTML Video tag

How to use HTML Video tag

Ravi Vishwakarma 262 06-Jun-2024

The <video> tag in HTML is used to attach video content to a webpage for visual representation of video content. Here's a guide on how to use it effectively:

Basic Syntax 

<video src="video.mp4" controls>
   Your browser does not support the video tag.
</video>

Video tag Attributes:

  1. src: Pass the path to the video file.
  2. controls: Attach video controls like play, pause, volume up and down, etc.
  3. width and height: Sets the dimensions of the video player on html page.
  4. autoplay: Automatic starts playing the video when the page loads.
  5. loop: Re-plays the video after it ends.
  6. muted: Mutes the video sound by default.
  7. poster: Specifies an image (like a thumbnail on video content) to be shown while the video is downloading or until the user hits the play button.
  8. preload: Suggests how the browser should load the video when the page loads. It can take the values auto, metadata, or none.

Example with Multiple Sources:
To ensure compatibility with different browsers, provide multiple video formats like (.mp4, .ogg, .mp3, .webm, etc) using the <source> tag:

<video width="640" height="360" controls>
   <source src="video.mp4" type="video/mp4">
   <source src="video.ogg" type="video/ogg">
   <source src="video.webm" type="video/webm">
   Your browser does not support the video tag.
</video>

Example with Additional Attributes:

<video width="640" height="360" controls autoplay loop muted poster="poster.jpg">
   <source src="video.mp4" type="video/mp4">
   <source src="video.ogg" type="video/ogg">
   <source src="video.webm" type="video/webm">
   Your browser does not support the video tag.
</video>

Using the <track> Tag: 
For accessibility, you can add subtitles or captions in your video using the <track> tag:
<video width="640" height="360" controls>
   <source src="video.mp4" type="video/mp4">
   <track src="subtitles_en.vtt" kind="subtitles" srclang="en" label="English">
   Your browser does not support the video tag.
</video>


Example with Fallback Content:
If the video tag is not supported, you can show fallback content to users that your device does not support video.

<video width="640" height="360" controls>
   <source src="video.mp4" type="video/mp4">
   <source src="video.ogg" type="video/ogg">
   <source src="video.webm" type="video/webm">
   Your browser does not support the video tag.
   <a href="video.mp4">Download the video</a> to view it.
</video>

By using the `<video>` tag with these attributes and techniques, you can effectively embed video content that is accessible and compatible across different web browsers.

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML Video Example</title>
</head>
<body>
    <h2>My Video</h2>
    <!-- The video tag is used to embed the video -->
    <!-- Width and height attributes specify the dimensions of the video player -->
    <!-- Controls attribute adds video controls like play, pause, volume, etc. -->
    <video width="640" height="360" controls>
        <!-- The source tag specifies the path to the video file and its MIME type -->
        <!-- This allows the browser to choose the appropriate video format based on its capabilities -->
        <source src="example_video.mp4" type="video/mp4">
        <!-- Fallback content displayed if the browser does not support the video tag or any of the video formats -->
        Your browser does not support the video tag.
    </video>
</body>
</html>

In this example:

  1. The <video> tag is used to attach the video. 
  2. The width and height attributes specify the dimensions of the video player. 
  3. The controls attribute attaches video controls like play, pause, volume, etc.
  4. Inside the <video> tag, a <source> tag is used to specify the path to the video file (example_video.mp4) and its MIME type (video/mp4). This allows the browser to choose the appropriate video format based on its capabilities.
  5. If the browser does not support the <video> tag or any of the video formats specified, the message "Your browser does not support the video tag." is displayed as fallback content.
  6. Make sure to replace "example_video.mp4" with the actual path to your video file.

 

How to use HTML Video tag

Updated 06-Jun-2024
Hi, my self Ravi Vishwakarma. I have completed my studies at SPICBB Varanasi. now I completed MCA with 76% form Veer Bahadur Singh Purvanchal University Jaunpur. SWE @ MindStick | Software Engineer | Web Developer | .Net Developer | Web Developer | Backend Engineer | .NET Core Developer

Leave Comment

Comments

Liked By